﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="JQuery.VSDoc.js" />
/// <reference path="JQuery.Extentions.js" />
/// <reference path="JQuery/popup.js" />
Type.prototype.registerEnumBase = Type.prototype.registerEnum;
Type.prototype.registerEnum = function Dimok$RegisterEnum(enumTypeName, extend) {
  this.registerEnumBase(enumTypeName);
  for (var e in this.prototype) {
    if (extend) this[e] = new Number(this[e]);
    this["get_" + e] = function(name) { return function() { return this[name] } } (e);
    this["get_" + e + "String"] = function(name) { return function() { return this.toString(this[name]) } } (e);
  }
}
Sys.EventArgs.prototype.addCancel = function() { if (this._cancel === undefined) { this._cancel = false; this.set_cancel = function(v) { this._cancel = v && true }; this.get_cancel = function() { return this._cancel } } }
Object.isFunction = function(value) { return Object.getType(value) == Function; }
Object.forEach = function Object$forEach(object, method, instance, accessorize) {
  /// <summary locid="M:J#Object.forEach" />
  /// <param name="object" type="object" elementMayBeNull="true"></param>
  /// <param name="method" type="Function(value,key,object)"></param>
  /// <param name="instance" optional="true" mayBeNull="true"></param>
  var e = Function._validateParams(arguments, [
        { name: "object", type: Object, elementMayBeNull: true },
        { name: "method", type: Function },
        { name: "instance", mayBeNull: true, optional: true },
        { name: "accessorize", type: Boolean, mayBeNull: true, optional: true }
    ]);
  if (e) throw e;
  for (var n in object) {
    if (Object.isFunction(object[n])) continue;
    var ret = method.call(instance, accessorize ? Dimok.Accessorize(object[n]) : object[n], n, object);
    if (ret) return ret;
  }
}
Object.forEachFunction = function Object$forEachFunction(object, method, instance, accessorize) {
  /// <summary locid="M:J#Array.forEach" />
  /// <param name="object" type="object" elementMayBeNull="true"></param>
  /// <param name="method" type="Function"></param>
  /// <param name="instance" optional="true" mayBeNull="true"></param>
  var e = Function._validateParams(arguments, [
        { name: "object", type: Object, elementMayBeNull: true },
        { name: "method", type: Function },
        { name: "instance", mayBeNull: true, optional: true },
        { name: "accessorize", type: Boolean, mayBeNull: true, optional: true }
    ]);
  if (e) throw e;
  for (var n in object) {
    if (Object.getType(object[n]) != Function) continue;
    var ret = method.call(instance, accessorize ? Dimok.Accessorize(object[n]) : object[n], n, object);
    if (ret) return ret;
  }
}

Object.capitalizeProperty = function Object$CapitalizeProperty(propertyName, object) {
  var re = new RegExp("^" + propertyName + "$", "i");
  for (var name in object)
    if (name.match(re)) return name;
  return '';
}
Object.firstValue = function(object) { for (var n in object) if (!Object.isFunction(object[n])) return object[n]; }
Object.firstName = function(object) { for (var n in object) if (!Object.isFunction(object[n])) return n; }
Date.prototype.toMSJSON = function() { return '"\\\/Date(' + this.getTime() + ')\\\/"' }
//function Dimok$MSDateToJSDate(data) { return data.replace(/"\\\/(Date\([0-9-]+\))\\\/"/gi, "new $1") }
// ************** Dimok namespace *******************
//window.Dimok = Function; eval("Dimok=null");
Type.registerNamespace("Dimok");

Dimok.IsHTML = function(text) { return !!(text + "").match(/^[\s\S]*</); }
Dimok.AddProperty = function Dimok$AddProperty(o, name, value, getPrefix, setPrefix) {
  if (typeof o == 'undefined') return o;
  if (value !== undefined) o[name] = value;
  if (isNaN(parseInt(name)) && typeof o[name] != "function") {
    var gp = getPrefix || "get" + _(name);
    var sp = setPrefix || "set" + _(name);
    if (o[gp + name] === undefined)
      o[gp + name] = function(propName) {
        return function() { return o[propName]; }
      } (name);
    if (o[sp + name] === undefined)
      o[sp + name] = function(propName) {
        return function(v) { return o[propName] = v; }
      } (name);
  }
  return o;
  function _(name) { return name.indexOf("_") == 0 ? "" : "_"; }
}
Dimok.Accessorize = Dimok$Accessorize;
Dimok.$A = Dimok$Accessorize;
function Dimok$Accessorize(o, makeCopy, getPrefix, setPrefix) {
  /// <summary locid="M:J#Dimok.Accessorize">Add get/set accessors to all properties</summary>
  /// <param name="o" type="object" elementMayBeNull="false"></param>
  if (typeof o == 'undefined') return o;
  if (makeCopy) o = $J.extend({}, o);
  for (var name in o)
    Dimok.AddProperty(o, name);
  return o;
}
Dimok.AccessorizeType = function Dimok$AccessorizeType(object) {
  var p = object.constructor.prototype;
  if (p.__isAccessorized) return;
  p.__isAccessorized = true;
  for (var name in object) {
    if (!isNaN(parseInt(name)) || typeof object[name] == "function") continue;
    var fix = _(name);
    var gp = "get" + fix;
    var sp = "set" + fix;
    if (object[gp + name] === undefined)
      p[gp + name] = function(propName) {
        return function() { return this[propName]; }
      } (name);
    if (object[sp + name] === undefined)
      p[sp + name] = function(propName) {
        return function(v) { return this[propName] = v; }
      } (name);
    if (name.endsWith("ID")) {
      var name1 = name.substr(0, name.length - 2);
      var fp = "find" + fix + name1; gp = "get" + fix + name1;
      if (object[fp] === undefined)
        p[fp] = function(propName) {
          return function() { return this[propName] ? $find(this[propName]) : null; }
        } (name);
      if (object[gp] === undefined)
        p[gp] = function(propName) {
          return function() { return $get(this[propName]); }
        } (name);
    }
  }
  function _(name) { return name.indexOf("_") == 0 ? "" : "_"; }
}
Dimok.CopyData = function Dimok$CopyData(o, dontAccessorize) {
  if (typeof o == 'undefined') return o;
  var r = {};
  for (var name in o)
    if (typeof o[name] != "function") r[name] = o[name];
  return dontAccessorize ? r : Dimok.Accessorize(r);
}

Dimok.ClearEvents = function Dimok$ClearEvents(component) {
  for (var events = component.get_events()._list; events.length; ) {
    var event = events.pop();
    while (event.length)
      event.pop();
  }
}
Dimok.ClearHandlers = function Dimok$ClearHandlers() {
  if (this._elementsWithHandlers === undefined)
    throw new Error(-1, Object.getType(this).__typeName + "._elementsWithHandlers is not defined in ");
  for (var e = 0; e < this._elementsWithHandlers.length; e++)
    $clearHandlers(this._elementsWithHandlers[e]);
}
Dimok.AddHandlers = function Dimok$AddHandlers(element, events, handlerOwner) {
  if (this._elementsWithHandlers === undefined)
    throw new Error(-1, Object.getType(this).__typeName+"._elementsWithHandlers is not defined in ");
  this._elementsWithHandlers.pop(element);
  $addHandlers(element, events, handlerOwner);
}
Dimok.DialogParams = function (w,h,t,l){
	w = w || screen.availWidth / 2;
	h = h || screen.availHeight / 2;
	t = t || (event||{}).screenY || (screen.availHeight-h)/2;
	l = l || (event||{}).screenX || (screen.availWidth -w)/2;
	return ["status=no","resizable=yes","dialogWidth="+w+"px","dialogHeight="+h+"px","dialogTop="+t+"px","dialogLeft="+l+"px"];
}

Dimok.FillSelectFromJSON = function Dimok$FillSelectFromJSON(selElement, jsonArray, textName, valName) {
  var sel = $J(selElement);
  sel.attr("length", 0);
  var rs = ($get(selElement.dataSrc.substr(1)) || {}).recordset;
  if (!jsonArray.length) {
    if (sel.attr("IfEmptyValue"))
      sel.html(_makeOption(sel.attr("IfEmptyText"), sel.attr("IfEmptyValue"), sel.attr("IfEmptyStyle")));
    if (rs) rs(selElement.dataFld) = selElement.value;
    return;
  }
  var text = Object.capitalizeProperty(textName || sel.attr("dataTxt") || sel.attr("dataText"), jsonArray[0]);
  var val = Object.capitalizeProperty(valName || sel.attr("dataVal") || sel.attr("dataValue") || sel.attr("dataFld"), jsonArray[0]);
  var style = Object.capitalizeProperty("style", jsonArray[0]);
  var selected = Object.capitalizeProperty("selected", jsonArray[0]);
  text = text || val;
  val = val || text;
  if (!val) val = text = Object.capitalizeProperty(sel.attr("dataFld"), jsonArray[0]);
  var options = '';
  Array.forEach(jsonArray, _fill, this);
  sel.html(options);
  if (sel.attr("length") > 1) sel.attr("selectedIndex", -1);
  if (sel.attr("length") == 1) selElement.selectedValue = selElement[0].value;
  try {
    if (rs) rs(selElement.dataFld) = selElement.value;
  } finally {
    rs = null;
  }
  function _fill(value) {
    options += _makeOption(value[text], value[val], value[style]);
  }
  function _makeOption(text, value, style, selected) {
    return String.format("<option value='{0}' style='{1}' {3}>{2}</option>", value || text || "", style || "", text || value || "", selected || "");
  }
}

Dimok.FormatPopUpMessage = function Dimok$FormatPopUpMessage(message, cols) {
  var reNL = new RegExp("(.{" + cols + "})", "g");
  return message.replace(/([.]\s)/g, "$1\n")
      .replace(/[.]{2,}/g, "\n")
      .replace(reNL, "$1\n")
      .replace(/\n/g, "<br>");
}

Dimok.AjaxSettings = {
  async: false,
  type: "POST",
  contentType: "application/json; charset=utf-8",
  dataFilter: function(data) { return data.replace(Sys.Serialization.JavaScriptSerializer._dateRegEx, "$1new Date($2)") },
  dataType: "json",
  error: function(XMLHttpRequest, textStatus, errorThrown) {
    if (XMLHttpRequest.status >= 500 && !Dimok.IsHTML(XMLHttpRequest.responseText))
      return alert(Sys.Serialization.JavaScriptSerializer.deserialize(XMLHttpRequest.responseText).Message);
    if ($J.popup) $J.popup.show("Server Request", XMLHttpRequest.responseText);
    else if ($J(".bubble")[0]) $J(".bubble").bubbleTip({ pos: { left: 200, top: 200 }, html: XMLHttpRequest.responseText });
    else alert(XMLHttpRequest.responseText);
  }
}
Dimok.Ajax = function Dimok$Ajax(settings, params, success, error, context) {
  /// <summary locid="M:J#Dimok.Ajax">Make a web service call</summary>
  /// <param name="settings" type="object" elementMayBeNull="false">
  ///   {methodName,wsPath,wsObject}
  /// </param>
  /// <param name="params" type="Array/Object" elementMayBeNull="true"></param>
  context = context || this;
  var wsPath = settings.wsPath ? _buildPath(settings.wsPath,settings.methodName) : Dimok.FindWSPath(settings.methodName, settings.wsObject);
  var data = {};
  if (Object.getType(params) == Array) {
    if (!settings.wsObject) throw "Array type [params] can not be used without [wsObject].";
    var wsMethod = Dimok.FindWSMethod(settings.methodName, settings.wsObject);
    var parameters = (wsMethod + "").match(/function\((.+?)\)/)[1].split(",")
    while (parameters.length > 2 && params.length > 0) {
      data[parameters.shift()] = params.shift();
    }
  } else data = params;
  var jsonString = Sys.Serialization.JavaScriptSerializer.serialize(data);
  if (error) error = Function.createCallback(error, context);
  var ret = null;
  $J.ajax($J.extend({}, Dimok.AjaxSettings, { url: wsPath, data: jsonString, success: success || _success, error: error }, settings));
  return ret;
  function _success(json) {
    ret = success ? Function.createCallback(success, context)(json) : json.d;
  }
  function _buildPath(path, name) {
    return path.match(/^(.+?)\/?$/)[1] + "/" + name;
  }
}
Dimok.FindMainMenu = function Dimok$MainMenu() {
  if (!Dimok || !Dimok.MainMenu) return null;
  for (var name in Sys.Application._components)
    if (Dimok.MainMenu.isInstanceOfType(Sys.Application._components[name]))
    return Sys.Application._components[name];
  return null;
}
Dimok.FindPageEvents = function Dimok$FindPageEvents() {
  if (!Dimok || !Dimok.PageEvents) return null;
  for (var name in Sys.Application._components)
    if (Dimok.PageEvents.isInstanceOfType(Sys.Application._components[name]))
    return Sys.Application._components[name];
  return $create(Dimok.PageEvents, { id: "PAGE_EVENTS" }, null, null, null);
}
Dimok.WSError = function Dimok$WSError(result, grid, arg2, arg3) {
  this._hideLoadPanel(this);
  alert(result._message);
}
Dimok.HideLoadPanel = Dimok$HideLoadPanel;
function Dimok$HideLoadPanel(sender, eventArgs) {
  sender = sender || this;
  $J(sender.get_element()).unblock();
}
Dimok.ShowLoadPanel = Dimok$ShowLoadPanel;
function Dimok$ShowLoadPanel(sender, message) {
  sender = sender || this;
  //var e = ($J("table:first", sender.get_element()).attr("tBodies") || {})[0] || sender.get_element();
  $J(sender.get_element()).block({
    message: message || "Loading ...",
    overlayCSS: { opacity: .05 },
    css: {
      left: "25%",
      border: '2px solid #aaa'
    },
    fadeOut: 200
  });
}
Dimok.RaiseEvent = function Dimok$RaiseEvent(eventName, eventArgs) {
  var handler = this.get_events().getHandler(eventName);
  if (handler) {
    if (!eventArgs) {
      eventArgs = Sys.EventArgs.Empty;
    }
    handler(this, eventArgs);
  }
}
Dimok.InitializeProperties = function Dimok$InitializeProperties(props) {
  var type = this.constructor.getName();
  var evalString = '';
  for (var name in props) {
    this[name] = props[name];
    if (!this["get" + name])
      this["get" + name] = function(propName) {
        return function() { return this[propName]; }
      } (name);
    if (!this["set" + name])
      this["set" + name] = function(propName) {
        return function(v) { return this[propName] = v; }
      } (name);
    if (name.endsWith("ID")) {
      var name1 = name.substr(0, name.length - 2);
      if (!this["get" + name1])
        this["get" + name1] = function(propName) {
          return function() { return $get(this[propName]); }
        } (name);
      if (!this["find" + name1])
        this["find" + name1] = function(propName) {
          return function() { return this[propName] ? $find(this[propName]) : null; }
        } (name);
    }
  }
}
Dimok.InitializeEvents = function Dimok$InitializeEvents(_3dd) {
  if (_3dd) {
    var _3de = this;
    for (var i = 0, l = _3dd.length; i < l; i++) {
      var name = _3dd[i];
      this["add_" + name] = function(_3e2) {
        return function(_3e3) {
          this.get_events().addHandler(_3e2, _3e3);
        };
      } (name);
      this["remove_" + name] = function(_3e4) {
        return function(_3e5) {
          this.get_events().removeHandler(_3e4, _3e5);
        };
      } (name);
      if (!this["raise_" + name])
        this["raise_" + name] = function(_3e6) {
          return function(args) {
            this.raiseEvent(_3e6, args);
          };
        } (name);
    }
  }
}
Dimok.FindWSMethod = function Dimok$FindWSMethod(methodName, globalWSO) {
  var methods = [];
  var method = Object.forEachFunction(globalWSO, function(ws, name) {
    if (ws[methodName]) return ws[methodName];
    methods.push(name);
  });
  if (!method) throw "Method " + methodName + " not found in " + Sys.Serialization.JavaScriptSerializer.serialize(methods);
  return method;
}
Dimok.FindWSPath = function Dimok$FindWSPath(methodName, globalWSO) {
  var methods = [];
  var wsPath = Object.forEachFunction(globalWSO, function(ws, name) {
    if (ws[methodName]) return ws.get_path() + "/" + methodName;
    methods.push(name);
  });
  if (!wsPath) {
    var message = "Method " + methodName + " not found in " + Sys.Serialization.JavaScriptSerializer.serialize(methods);
    alert(message);
    throw Error.create(message);
  }
  return wsPath;
}
// ************** Control ******************
Type.registerNamespace("motobankcorp.UI");
motobankcorp.UI.Control = function(element) {
  motobankcorp.UI.Control.initializeBase(this, [element]);
  this._initProps({ _isLoaded: true });
  this.get_wsErrorDelegate = function() { return Function.createDelegate(this, Dimok.WSError) };
}
motobankcorp.UI.Control.prototype = {
  initialize: function() {
    motobankcorp.UI.Control.callBaseMethod(this, 'initialize');
    Dimok.AccessorizeType(this);
  },
  dispose: function() {
    Dimok$ClearEvents(this);
    $clearHandlers(this.get_element());
    Dimok.ClearHandlers.apply(this);
    motobankcorp.UI.Control.callBaseMethod(this, 'dispose');
  },
  _elementsWithHandlers: [],
  addHandlers: Dimok.AddHandlers,
  clearHandlers:Dimok.ClearHandlers,
  raiseEvent: Dimok.RaiseEvent,
  _initProps: Dimok.InitializeProperties,
  _initEvents: Dimok.InitializeEvents,
  _hideLoadPanel: Dimok.HideLoadPanel,
  _showLoadPanel: Dimok.ShowLoadPanel,
  findPageEventsGlobal: Dimok.FindPageEvents,
  get_PE: function() { return Dimok.FindPageEvents(); }
}
motobankcorp.UI.Control.registerClass('motobankcorp.UI.Control', Sys.UI.Control);
// ************** Behavior ******************
Type.registerNamespace("motobankcorp.UI");
motobankcorp.UI.Behavior = function(element) {
  motobankcorp.UI.Behavior.initializeBase(this, [element]);
  this.get_wsErrorDelegate = function() { return Function.createDelegate(this, Dimok.WSError) };
}
motobankcorp.UI.Behavior.prototype = {
  initialize: function() {
    motobankcorp.UI.Behavior.callBaseMethod(this, 'initialize');
    Dimok.AccessorizeType(this);
  },
  dispose: function() {
    Dimok$ClearEvents(this);
    $clearHandlers(this.get_element());
    Dimok.ClearHandlers.apply(this);
    motobankcorp.UI.Behavior.callBaseMethod(this, 'dispose');
  },
  _elementsWithHandlers: [],
  addHandlers: Dimok.AddHandlers,
  clearHandlers: Dimok.ClearHandlers,
  raiseEvent: Dimok.RaiseEvent,
  _initProps: Dimok.InitializeProperties,
  _initEvents: Dimok.InitializeEvents,
  _hideLoadPanel: Dimok.HideLoadPanel,
  _showLoadPanel: Dimok.ShowLoadPanel,
  findPageEventsGlobal: Dimok.FindPageEvents,
  get_PE: function() { return Dimok.FindPageEvents(); }
}
motobankcorp.UI.Behavior.registerClass('motobankcorp.UI.Behavior', Sys.UI.Behavior);
// ************** Component ******************
Type.registerNamespace("motobankcorp");
motobankcorp.Component = function() {
  motobankcorp.Component.initializeBase(this);
  this.get_wsErrorDelegate = function() { return Function.createDelegate(this, Dimok.WSError) };
}
motobankcorp.Component.prototype = {
  initialize: function() {
    motobankcorp.Component.callBaseMethod(this, 'initialize');
    Dimok.AccessorizeType(this);
  },
  dispose: function() {
    Dimok$ClearEvents(this);
    Dimok.ClearHandlers.apply(this);
    motobankcorp.Component.callBaseMethod(this, 'dispose');
  },
  _elementsWithHandlers: [],
  addHandlers: Dimok.AddHandlers,
  clearHandlers: Dimok.ClearHandlers,
  raiseEvent: Dimok.RaiseEvent,
  _initProps: Dimok.InitializeProperties,
  _initEvents: Dimok.InitializeEvents,
  _hideLoadPanel: Dimok.HideLoadPanel,
  _showLoadPanel: Dimok.ShowLoadPanel,
  findPageEventsGlobal: Dimok.FindPageEvents,
  get_PE: function() { return Dimok.FindPageEvents(); }
}
motobankcorp.Component.registerClass('motobankcorp.Component', Sys.Component);
// ******************************************************************************
